using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Framework.Event{

    public delegate void  FuncDelegate();
    public delegate void  FuncDelegate<A>(A a);
    public delegate void  FuncDelegate<A, B>(A a, B b);
    public delegate void  FuncDelegate<A, B, C>(A a ,B b, C c);


    /// 信号槽
    public class Signal:Framework.Base.BaseObject, Framework.Base.IPoolObj
    {

        public int signalCnt = 0;

        public event FuncDelegate m_funcDelegate;
        

        // 注册事件
        public virtual void On(FuncDelegate obj){
            this.m_funcDelegate += obj;
            this.signalCnt ++;
        }

        // 取消事件监听
        public virtual void Off(FuncDelegate obj){
            this.m_funcDelegate -= obj;
            this.signalCnt --;
        }

        // 发送事件
        public virtual void Emit(){
            this.m_funcDelegate.Invoke();
        }

        // 清理全部信号
        public void Clear(){
            if(this.signalCnt > 0){
                foreach(FuncDelegate del in this.m_funcDelegate.GetInvocationList()){
                    this.m_funcDelegate -= del;
                }
                this.signalCnt = 0;
            }
        }

        //  移除一个对象的全信号
        public void RemoveObj(object obj){
            if(this.signalCnt > 0){
                foreach(FuncDelegate del in this.m_funcDelegate.GetInvocationList()){
                    if(del.Target == obj){
                        this.m_funcDelegate -= del;
                    }
                    
                }
                this.signalCnt = 0;
            }
        }
        
        public bool isEmpty(){
            return this.signalCnt == 0;
        }

        public void OnFetch(){

        }

        // 加入回收池
        public void OnRecycle(){
            this.Clear();
        }

    }

    public class Signal<A>:Signal{
        
        public new event FuncDelegate<A> m_funcDelegate;

         // 发送事件
        public void Emit(A a){
            this.m_funcDelegate.Invoke(a);
        }
        
        public void On(FuncDelegate<A> obj){
            this.m_funcDelegate += obj;
            this.signalCnt ++;
        }

        // 取消事件监听
        public void Off(FuncDelegate<A> obj){
            this.m_funcDelegate -= obj;
            this.signalCnt --;
        }
        
    }

    public class Signal<A, B>:Signal{
        public new event FuncDelegate<A,B> m_funcDelegate;

        
         // 发送事件
        public void Emit(A a, B b){
            this.m_funcDelegate.Invoke(a, b);
        }
        
        public void On(FuncDelegate<A,B> obj){
            this.m_funcDelegate += obj;
            this.signalCnt ++;
        }

        // 取消事件监听
        public void Off(FuncDelegate<A, B> obj){
            this.m_funcDelegate -= obj;
            this.signalCnt --;
        }
    }

    public class Signal<A, B, C>:Signal{
        public new  event FuncDelegate<A,B,C> m_funcDelegate;

        public void Emit(A a, B b, C c){
            this.m_funcDelegate.Invoke(a, b, c);
        }
        
        public void On(FuncDelegate<A,B,C> obj){
            this.m_funcDelegate += obj;
            this.signalCnt ++;
        }

        // 取消事件监听
        public void Off(FuncDelegate<A, B,C> obj){
            this.m_funcDelegate -= obj;
            this.signalCnt --;
        }
    }

}